//
//  ChangePwdViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/16.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class ChangePwdViewController: BaseViewController {

    @IBOutlet var oldPwdTF: UITextField!
    
    @IBOutlet var newPwdTF: UITextField!
    
    @IBOutlet var newSurePwdTF: UITextField!
    
    @IBOutlet var changePwdBtn: UIButton!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func changePwdBtnPress(_ sender: UIButton) {
        
        if (oldPwdTF.text == "") || (self.newPwdTF.text == "") || (self.newSurePwdTF.text == "") {
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        if (self.newPwdTF.text != self.newSurePwdTF.text) {
             MBProgressHUD.alertShow(in: self.view, message: "请填写相同的新密码", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requestChangePwd(oldPwd: oldPwdTF.text!, newPwd: newPwdTF.text!)
    }
    
    
    //网络请求
    //修改密码
    func requestChangePwd(oldPwd:String,newPwd:String) -> Void {
        
        let param =
            ["uid":UserModel.shareUser.data?.uid ?? String(),
             "password":oldPwd,
             "newPassword":newPwd
                ]
        
        Alamofire.request(changePwdUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        print("数据请求成功")
                        self.navigationController?.popViewController(animated: true)
                        
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
